import datetime
import time
from datetime import timedelta, timezone


def str_is_chn(string):
    """
    检查整个字符串是否包含中文
    :param string: 需要检查的字符串
    :return: bool
    """
    for ch in string:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True

    return False


def str_is_hex(string):
    """
    检查整个字符串是否是16进制
    """
    hexdigits = '0123456789' + 'abcdef' + 'ABCDEF'
    for c in string:
        if c not in hexdigits:
            return False
    if len(string) % 2 != 0:
        return False
    return True


def str_is_digit(string):
    """
    检查整个字符串是否是10进制
    """
    digits = '0123456789'
    for c in string:
        if c not in digits:
            return False
    return True


def tmivl_arrived(old_tm, tmout_s):
    """
    判断间隔时间是否到达
    """
    if old_tm == 0:
        return True
    tmivl = datetime.datetime.now() - old_tm
    if tmivl.total_seconds() >= tmout_s:
        return True
    return False


def inttm_to_cntm(tm_int):
    """
    时间戳转成北京时间
    """
    cst_time = ""
    try:
        utc_time = datetime.datetime.fromtimestamp(tm_int)
        cst_time = utc_time.astimezone(timezone(timedelta(hours=0))).strftime("%Y-%m-%d %H:%M:%S")
    except Exception as e:
        print("inttm_to_cntm err", e)
    return cst_time


def cntm_to_inttm(tm_str):
    """
    北京时间转成时间戳
    """
    tm_int = 0
    try:
        utc_time = datetime.datetime.strptime(tm_str, "%Y-%m-%d %H:%M:%S")
        cst_time = utc_time.astimezone(timezone(timedelta(hours=+16))).strftime("%Y-%m-%d %H:%M:%S")
        tm_int = int(time.mktime(time.strptime(cst_time, "%Y-%m-%d %H:%M:%S")))
    except Exception as e:
        print("cntm_to_inttm err", e)
    return tm_int


def list_hexprint(list_data):
    """
    列表16进制打印输出
    """
    a = [hex(i) for i in list_data]
    print(a)